/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.utils.excel.ExcelExport;
import com.jeesite.modules.kj.entity.KjMember;
import com.jeesite.modules.kj.utils.SystemLogUtil;
import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.Log;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.utils.EmpUtils;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjQuestion;
import com.jeesite.modules.kj.service.KjQuestionService;

import java.util.List;

/**
 * kj_questionController
 * @author hzy
 * @version 2021-07-19
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjQuestion")
public class KjQuestionController extends BaseController {

	@Autowired
	private KjQuestionService kjQuestionService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjQuestion get(String id, boolean isNewRecord) {
		return kjQuestionService.get(id, isNewRecord);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjQuestion:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjQuestion kjQuestion, Model model) {
		model.addAttribute("kjQuestion", kjQuestion);
		return "modules/kj/kjQuestionList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjQuestion:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjQuestion> listData(KjQuestion kjQuestion, HttpServletRequest request, HttpServletResponse response) {
		kjQuestion.setPage(new Page<>(request, response));
		User currentUser = UserUtils.getUser();
		if(!currentUser.isSuperAdmin()) {
			kjQuestion.setUserCode(currentUser.getUserCode());
		}
		Page<KjQuestion> page = kjQuestion.getPage();
		page.setList(kjQuestionService.findListByRegions(kjQuestion));
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjQuestion:view")
	@RequestMapping(value = "form")
	public String form(KjQuestion kjQuestion, Model model) {
		model.addAttribute("kjQuestion", kjQuestion);
		return "modules/kj/kjQuestionForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjQuestion:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjQuestion kjQuestion, HttpServletRequest request) {
		KjQuestion oldData = kjQuestionService.get(kjQuestion.getId());
		if(!oldData.getReplyStatus().equals(kjQuestion.getReplyStatus())){
			if(kjQuestion.getReplyStatus().equals(KjQuestion.REPLY)){
				SystemLogUtil.insertAdminOperate(UserUtils.getUser(), "问题反馈", Log.TYPE_UPDATE, "已反馈", kjQuestion.getMemberName() + "反馈的" + "\"" + kjQuestion.getTitle() + "\"", request);
			}
		}
		kjQuestionService.save(kjQuestion);
		return renderResult(Global.TRUE, text("保存问题反馈成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjQuestion:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjQuestion kjQuestion) {
		kjQuestionService.delete(kjQuestion);
		return renderResult(Global.TRUE, text("删除问题反馈成功！"));
	}

	/**
	 * 列表选择对话框
	 */
	@RequiresPermissions("kj:kjQuestion:view")
	@RequestMapping(value = "kjQuestionSelect")
	public String empUserSelect(KjQuestion kjQuestion, String selectData, Model model) {
		String selectDataJson = EncodeUtils.decodeUrl(selectData);
		if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()){
			model.addAttribute("selectData", selectDataJson);
		}
		model.addAttribute("KjQuestion", kjQuestion);
		return "modules/kj/kjQuestionSelect";
	}


	/**
	 * 导出问题数据
	 */
	@RequiresPermissions("kj:kjQuestion:view")
	@RequestMapping(value = "exportData")
	public void exportData(KjQuestion kjQuestion, Boolean isAll, String ctrlPermi, HttpServletResponse response) {
		kjQuestion.setPageNo(null);
		kjQuestion.setPageSize(null);
		List<KjQuestion> list = kjQuestionService.findList(kjQuestion);
		String fileName = "问题反馈数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
		try(ExcelExport ee = new ExcelExport("问题反馈数据", KjQuestion.class)){
			ee.setDataList(list).write(response, fileName);
		}
	}

}
